import random
import time

def load_lines(file_path):
    """Read lines from a file and return them."""
    try:
        with open(file_path, "r") as file:
            lines = file.readlines()
        return lines
    except FileNotFoundError:
        print(f"The file {file_path} does not exist.")
        return []

def select_random_lines(lines):
    """Select two random lines from the given list of lines."""
    line_count = len(lines)
    if line_count < 1:
        print("The file doesn't have enough lines.")
        return None, None
    idx1 = random.randint(0, line_count - 1)
    idx2 = random.randint(0, line_count - 1)
    return lines[idx1].strip(), lines[idx2].strip()

def guessing_game():
    """Run the guessing game loop."""
    file_path = "numbers.txt"
    lines = load_lines(file_path)
    
    if not lines:
        return
    
    correct_guesses = 0  # Counter for correct guesses

    while True:
        line1, line2 = select_random_lines(lines)
        
        if line1 is None or line2 is None:
            break
        
        print(f"证件1: {line1}")
        print(f"证件2: {line2}")
        
        user_guess = input("他是你的邻居吗?(yes/no): ").strip().lower()
        
        if (line1 == line2 and user_guess == "yes") or (line1 != line2 and user_guess == "no"):
            correct_guesses += 1  # Increment correct guess count
            time.sleep(1)
            print("答对了!")
        else:
            time.sleep(1)
            print("答错了，你被'邻居'杀死了！")
        
        time.sleep(1)
        
        if correct_guesses >= 20:
            print("你赢了！ By D.D.D. and Tom")
            break  # End the game when the player wins

if __name__ == "__main__":
    guessing_game()
